var e = require("utils/core.js");
App({
  onLaunch: function () {
    //var that=this,
      // e = that.getCache("userinfo");
      // "" == e && that.getUserInfo(function (e) { }, function (e, t) {
      //   var t = t ? 1 : 0,
      //     e = e || "";
      //   // t && wx.redirectTo({
      //   //   url: "/pages/error/index?close=" + t + "&text=" + e
      //   // })
      // })
  },
  requirejs: function (e) {
    return require("utils/" + e + ".js")
  },

  getUserInfo: function (t, i) {
    var n = this,
      openid='',
      sessionKey='',
      a = n.getCache("userinfo");
    if (a) return void (t && "function" == typeof t && t(a));
    wx.login({
      success: function (o) {
        if (!o.code) return void e.alert("获取用户登录态失败2");
        e.get("Shop/openid", {
          wx_code: o.code
        }, function (o) {
        if(o.code==0){
          if (!o.info.errcode) {
            openid = o.info.openid;
            sessionKey = o.info.session_key;
          } else {
            e.alert('errcode:' + o.info.errcode + ';errmsg:' + o.info.errmsg);
            return false;
          }
        }else{
          e.alert(o.msg);
          return false;
        }
          
          wx.getUserInfo({
            success: function (i) {
              console.log(i)
              a = i.userInfo,
                e.get("Shop/Login", {
                wx_openid:openid,
                nick_name: i.userInfo.nickName,
                user_headimg: i.userInfo.avatarUrl
              }, function (e) {
                i.userInfo.openid = openid, i.userInfo.uid = e.info, n.setCache("userinfo", i.userInfo), t && "function" == typeof t && t(a)
              })
            },
            fail: function () {
              console.log('获取用户信息失败1')
            }
          })
        })
      },
      fail: function () {
        e.alert("获取用户信息失败")
      }
    })
  },
  /*
    *获取缓存
     * @param e 缓存名称
     * @param t 缓存值
  */
  getCache: function (e, t) {
    var i = +new Date / 1000,
      n = "";
    i = parseInt(i);
    try {
      n = wx.getStorageSync(e + this.globalData.appid), n.expire > i || 0 == n.expire ? n = n.value : (n = "", this.removeCache(e))
    } catch (e) {
      n = void 0 === t ? "" : t
    }
    return n = n || ""
  },
   /*
    *创建缓存
     * @param e 缓存名称
     * @param t 缓存值
     * @param i 缓存时间
  */
  setCache: function (e, t, i) {
    var n = +new Date / 1000,
      a = true,
      o = {
        expire: i ? n + parseInt(i) : 0,
        value: t
      };
    try {
      wx.setStorageSync(e + this.globalData.appid, o)
    } catch (e) {
      a = false
    }
    return a
  },
  /*
   *清除缓存
   * @param e 缓存名称
   */
  removeCache: function (e) {
    var t = true;
    try {
      wx.removeStorageSync(e + this.globalData.appid)
    } catch (e) {
      t = false
    }
    return t
  },
  globalData: {
    appid: 'wx16b60e86d1f447ed',
    api: "https://hykapi.sssvip.net/",
    userInfo: null,
    app_name:'壹佰会员卡',
    shop_id:0
  },
  config: {
    background: '#df2f22',//个人汇中心背景#df2f22
    button_color: '#df2f22',//商品详情按钮，
    alert_color: '#df2f22',//信息提示，
  }
})